﻿
/*
EC-IDE class: RuleCompiler

Author: Feng Weiguo ( 冯卫国 )
Mail: forxm@21cn.com
Web: www.supertree.org

All Rights Reserved, 2008-9

LICENSE: LGPL

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
*/

$ec.$preload(
	"org.supertree.editor.RuleCompiler",
	function()
	{
		this.prototype=
		{
//////////////////////////////
// EC-CLASS BODY
//////////////////////////////
			RuleCompiler: function()
			{
				throw "can not instantiate RuleCompiler";
			},
			
			/*
			_compile will be called by rule item, and will eval the "this.xxx" string in rule item.
			*/
			_compile: function( rule )
			{
				var r,i;
				
				if( rule[0] && typeof(rule[0])=="string" )
				{
					r= new RegExp;
					if( rule.regFlags )	//regFlags: default is "g"
					{
						r.compile( rule[0], rule.regFlags );
					}
					else
					{
						r.compile( rule[0], "g" );
					}
					
					rule[0]= r;
				}
				else return;
				
				for(i=1;i<rule.length;i++)
				{
					if( rule[i][2]>0 )
					{
						if( typeof(rule[i][4])=="string" ) rule[i][4]=eval(rule[i][4]);
						arguments.callee.call( this, rule[i][4] );
					}
				}
			},
			
			//oRule: rule class prototype
			Compile: function( oRule )
			{
				this._compile.call( oRule, oRule.rule )
			},
			
			//format a string to IgnoreCase reg string, ex, abc=>[aA][aB][cC]
			ToIgnoreCase: function(str)
			{
				/*
				var s1=str.toLowerCase();
				var s2=str.toUpperCase();
				var i,imax;
				imax=s1.length;
				var sa=[];
				for(i=0;i<imax;i++)
				{
					if( s1.charAt(i)!=s2.charAt(i) )
					{
						sa[sa.length]= "["+s1.charAt(i)+s2.charAt(i)+"]";
					}
					else sa[sa.length]= s1.charAt(i);
				}
				//alert(sa.join(""));
				return sa.join("");
				*/
				
				//speed up
				return str.toLowerCase()
					.replace(/a/g,'[aA]')
					.replace(/b/g,'[bB]')
					.replace(/c/g,'[cC]')
					.replace(/d/g,'[dD]')
					.replace(/e/g,'[eE]')
					.replace(/f/g,'[fF]')
					.replace(/g/g,'[gG]')
					.replace(/h/g,'[hH]')
					.replace(/i/g,'[iI]')
					.replace(/j/g,'[jJ]')
					.replace(/k/g,'[kK]')
					.replace(/l/g,'[lL]')
					.replace(/m/g,'[mM]')
					.replace(/n/g,'[nN]')
					.replace(/o/g,'[oO]')
					.replace(/p/g,'[pP]')
					.replace(/q/g,'[qQ]')
					.replace(/r/g,'[rR]')
					.replace(/s/g,'[sS]')
					.replace(/t/g,'[tT]')
					.replace(/u/g,'[uU]')
					.replace(/v/g,'[vV]')
					.replace(/w/g,'[wW]')
					.replace(/x/g,'[xX]')
					.replace(/y/g,'[yY]')
					.replace(/z/g,'[zZ]');
				//alert(str.length);
				//return str;
			},
			
			//refer ext 2.0.2
		   createStyleSheet : function(doc, cssText){
		       var ss;
		       var head = doc.getElementsByTagName("head")[0];
		       var rules = doc.createElement("style");
		       rules.setAttribute("type", "text/css");

		       if(window.ActiveXObject){
		           head.appendChild(rules);
		           ss = rules.styleSheet;
		           ss.cssText = cssText;
		       }else{
		           try{
		                rules.appendChild(doc.createTextNode(cssText));
		           }catch(e){
		               rules.cssText = cssText;
		           }
		           head.appendChild(rules);
		           ss = rules.styleSheet ? rules.styleSheet : (rules.sheet || doc.styleSheets[doc.styleSheets.length-1]);
		       }
		       return ss;
		   },
			
			//default color style
			aColor: ["Red","Blue","Green","Gray","Brown","Fuchsia","Purple"],
			
			//creat color style
			//doc: the window.document
			ColorStyle: function( a, doc, bString )
			{
				var i,s;
				if(!doc) doc=document;
				
				/*
				//alert(doc.styleSheets);
				var sheet;
				if(doc.styleSheets)
				{
					if( doc.styleSheets.length>0 && window.ActiveXObject ) sheet= doc.styleSheets[0];
					//if( doc.styleSheets.length>0 ) sheet= doc.styleSheets[0];
					//else if( doc.createStyleSheet ) sheet= doc.createStyleSheet();
					
					//if( doc.createStyleSheet ) sheet= doc.createStyleSheet();
					//else if( doc.styleSheets.length>0 ) sheet= doc.styleSheets[0];
				}
				*/
				
				var sB= "font-weight:bold;letter-spacing:0px;"+(window.ActiveXObject?"*padding-right:1px;":"");
				var sU= "text-decoration:underline;";
				var sRed= "color:red;";
				
				var sa=[];
				
				for(i=0;i<a.length;i++)
				{
					if(i>0 && a[i]==a[i-1]) continue;	//remove duplicated
					
					//if(window.ActiveXObject || !sheet || bString )
					//{
						sa[sa.length]= "."+a[i]+"{color:"+a[i]+";}";
						sa[sa.length]= "."+a[i]+"B{color:"+a[i]+";"+sB+"}";
						sa[sa.length]= "."+a[i]+"U{color:"+a[i]+";"+sU+"}";
						sa[sa.length]= "."+a[i]+"UB{color:"+a[i]+";"+sU+sB+"}";
					//}
					/*
					else
					{
						sheet.insertRule("."+a[i]+"{color:"+a[i]+";}",0);
						sheet.insertRule("."+a[i]+"B{color:"+a[i]+";"+sB+"}",0);
						sheet.insertRule("."+a[i]+"U{color:"+a[i]+";"+sU+"}",0);
						sheet.insertRule("."+a[i]+"UB{color:"+a[i]+";"+sU+sB+"}",0);
					}
					*/
				}
				
				if(a==this.aColor)	//only 1 time
				{
					s= ".return{font-size:5px;border-right:1px solid LIGHTBLUE;border-bottom:1px solid LIGHTBLUE;width:1px;padding-left:4px;height:5px;}";
					if(window.opera) s=s.slice(0,-1)+"display:inline-block;height:1em;}";		//for Opera9.6: ol/li will collapse when li contain empty string
					
					//var s2= ".SupertreeTextEditor li{display:block;}";
					
					//if(window.ActiveXObject || !sheet || bString )
					//{
						sa[sa.length]= ".err{"+sRed+sB+"}";
						sa[sa.length]= ".errx{"+sRed+sB+"}";
						sa[sa.length]= ".alert{"+sRed+"}";
						
						sa[sa.length]= ".trace{"+sRed+sB+sU+"}";
						sa[sa.length]= ".trace2{"+sRed+sB+"text-decoration:line-through;"+"}";
						sa[sa.length]= ".trace3{"+sRed+sB+"text-decoration:overline;"+"}";
						sa[sa.length]= s;
						//sa[sa.length]= s2;
					//}
					/*
					else
					{
						sheet.insertRule(".err{"+sRed+sB+"}",0);
						sheet.insertRule(".errx{"+sRed+sB+"}",0);
						sheet.insertRule(".alert{"+sRed+"}",0);
						
						sheet.insertRule(".trace{"+sRed+sB+sU+"}",0);
						sheet.insertRule(".trace2{"+sRed+sB+"text-decoration:line-through;"+"}",0);
						sheet.insertRule(".trace3{"+sRed+sB+"text-decoration:overline;"+"}",0);
						sheet.insertRule(s,0);
						//sheet.insertRule(s2,0);
					}
					*/
				}
				
				if( bString ) return sa.join("");
				
				this.createStyleSheet( doc, sa.join("") );
				/*
				//speed up in ie6
				if(window.ActiveXObject) sheet.cssText+= sa.join("");
				else if(!sheet)
				{
					
					//alert("h");
					//sheet=doc.createElement("style");
					//doc.body.appendChild(sheet).innerHTML=sa.join("");
					//doc.body.insertBefore(sheet,doc.body.firstChild).innerHTML=sa.join("");
				}
				*/
			}
			
//////////////////////////////
// END EC-CLASS BODY	//NOTE - DO NOT CHANGE THESE 3 LINES, the ide wizards may use them.
//////////////////////////////
		}
		
		this.prototype.aColor.sort();	//for removing duplicated
		//aaa
		this.prototype.ColorStyle(this.prototype.aColor);
		
		//void CSS to ensure app containing at least 1 style
		
		//#EC_RES
		////#CSS: css_RuleCompiler, RuleCompiler.css
		//#END_EC_RES
		
	}
);
